<?php
// +----------------------------------------------------------------------
// | RXThinkCMF_EVTP5.1_PRO前后端分离旗舰版框架 [ RXThinkCMF ]
// +----------------------------------------------------------------------
// | 版权所有 2018~2021 南京RXThinkCMF研发中心
// +----------------------------------------------------------------------
// | 官方网站: http://www.rxthink.cn
// +----------------------------------------------------------------------
// | 作者: ZhangXuanNeng <rxthinkcmf@163.com>
// +----------------------------------------------------------------------
// | 免责声明:
// | 本软件框架禁止任何单位和个人用于任何违法、侵害他人合法利益等恶意的行为，禁止用于任何违
// | 反我国法律法规的一切平台研发，任何单位和个人使用本软件框架用于产品研发而产生的任何意外
// | 、疏忽、合约毁坏、诽谤、版权或知识产权侵犯及其造成的损失 (包括但不限于直接、间接、附带
// | 或衍生的损失等)，本团队不承担任何法律责任。本软件框架只能用于公司和个人内部的法律所允
// | 许的合法合规的软件产品研发，详细声明内容请阅读《框架免责声明》附件；
// +----------------------------------------------------------------------

namespace app\admin\service;


use app\admin\model\BanLog;
use app\admin\model\BoxArticle;
use app\admin\model\BoxBanRule;
use app\admin\model\BoxReport;
use app\article\service\CommentService;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;

/**
 * 举报记录管理-服务类
 * @author ZhangXuanNeng
 * @since: 2023/10/13
 * Class BoxReportService
 * @package app\admin\service
 */
class BoxReportService extends BaseService
{
    /**
     * 构造函数
     * LevelService constructor.
     */
    public function __construct()
    {
        $this->model = new BoxReport();
    }

    /**
     * 获取数据列表
     * @return array
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @since 2023/10/13
     * @author ZhangXuanNeng
     */
    public function getList(): array
    {
        $param = request()->param();
        // 查询条件
        $map = [];
        if(isset($param['report_type']) && !empty($param['report_type'])){
            $map[] = ['report_type','=',$param['report_type']];
        }
        if(isset($param['type']) && !empty($param['type'])){
            $map[] = ['type','=',$param['type']];
        }

        if (isset($param['status']) && is_numeric($param['status'])) {
            $map[] = ['status','=',$param['status']];
        }
        if (isset($param['id']) && is_numeric($param['id'])) {
            $map[] = ['id','=',$param['id']];
        }
        if (isset($param['post_platform']) && is_numeric($param['post_platform'])) {
            $map[] = ['post_platform','=',$param['post_platform']];
        }

        //时间
        $timeRange = $param['time_range'] ?? [];
        if ($timeRange) {
            $map[] = ['create_time', 'between', $timeRange];
        }
        $list = $this->model->where($map)->order('id desc')->paginate(10)->toArray();

        foreach ($list['data'] as &$val){
            $array[] = $this->model->getReportList($val);
        }
        //返回结果
        return [
            "msg"   => '操作成功',
            "code"  => 0,
            "data"  => $array ?? [],
            "count" => $list['total'],
        ];
    }

    public function handleReport($userId)
    {
        $postData = request()->post();
        $record = BoxReport::get((int)$postData['id']);
        if (!$record || $record['status'] != BoxReport::STATUS_UNHANDLED) {
            return message("该记录已处理",false);
        }

        $record->deal_userid = $userId;
        $record->deal_time = date('Y-m-d H:i:s');
        if ($postData['status'] == BoxReport::STATUS_NOT_GUILT) {
            $record->status = BoxReport::STATUS_NOT_GUILT;
            $record->save();
        } elseif ($postData['status'] == BoxReport::STATUS_GUILT) {
            $record->status = BoxReport::STATUS_GUILT;
            //是否封禁等
            if ($postData['deal_status'] == BoxReport::DEAL_STATUS_PUNISH) {
                if(isset($postData['ban_rule_id']) &&!empty($postData['ban_rule_id'])){
                    $record->ban_rule_id = (int)$postData['ban_rule_id'];
                    $banRule = BoxBanRule::get((int)$postData['ban_rule_id']);
                    $rule = json_decode($banRule['rule'], true);
                    if (isset($rule['ban_comment']) && $rule['ban_comment']) {
                        BanLogService::ban($record['target_member_id'], BanLog::TYPE_COMMENT, $rule['ban_comment'] * 24 * 3600);
                    }
                    if (isset($rule['ban_member']) && $rule['ban_member']) {
                        //封禁账号
                        BanLogService::ban($record['target_member_id'], BanLog::TYPE_ACCOUNT, $rule['ban_member'] * 24 * 3600);
                    }
                }
                $record->deal_status = BoxReport::DEAL_STATUS_PUNISH;
            }
            //是否删除内容
            if ($postData['is_del_content'] == BoxReport::DEL_CONTENT) {
                //删除评论等
                switch ($postData['type']) {
                    case BoxReport::TYPE_COMMENT:
                        CommentService::destroyComment($record->target_id);
                        break;
                    case BoxReport::TYPE_TOPIC:
                        $articleId = $record->target_id;
                        BoxArticle::destroy(function ($query) use ($articleId) {
                            $query->where('id', '=', $articleId);
                        });
                        break;
                }
                $record->is_del_content = BoxReport::DEL_CONTENT;
            }

            $record->save();
        }
        return message("处理成功!");
    }


}
